<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TwitterMatic.Shared.Domain.TimedTweet>" %>

<asp:Content ID="title" ContentPlaceHolderID="TitleContent" runat="server">
	Schedule a tweet for <%=User.Identity.Name%>
</asp:Content>

<asp:Content ID="content" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        $(function() {
            $("#SendOnDate").datepicker({ minDate: 0, showAnim: 'slideDown', dateFormat: 'mm/dd/yy' });
            $("#SendOnTime").timepickr({ convention: 12, rangeMin: ['00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55'] });

            calcSendOnTime();
            calcUtcOffset();

            calcCharsLeft();
            $("#Status").keyup(function() {
                calcCharsLeft();
            });
        });

        var calcCharsLeft = function() {
            var charsLeft = (140 - $("#Status").val().length);

            $("#status-chars-left").html(charsLeft);
            if (charsLeft < 0) {
                $("#status-chars-left").css('color', 'red');
                $("#status-chars-left").css('font-weight', 'bold');
            } else {
                $("#status-chars-left").css('color', 'white');
                $("#status-chars-left").css('font-weight', 'normal');
            }
        }

        var leadingZero = function(val) {
            if (val < 10)
                val = "0" + val;
            return val;
        }

        var calcSendOnTime = function() {
            var now = new Date();
            var offset = now.getTimezoneOffset();
            var stringDate = $("#SendOnDate").val() + ' ' + $("#SendOnTime").val();
            var parsedDate = Date.parse(stringDate);

            if (parsedDate < now) {
                var curr_hour = now.getHours();
                if (curr_hour < 12) {
                    a_p = "am";
                }
                else {
                    a_p = "pm";
                }
                if (curr_hour == 0) {
                    curr_hour = 12;
                }
                if (curr_hour > 12) {
                    curr_hour = curr_hour - 12;
                }

                $("#SendOnDate").val(leadingZero(now.getMonth() + 1) + '/' + leadingZero(now.getDate()) + '/' + now.getFullYear());
                $("#SendOnTime").val(leadingZero(curr_hour) + ':' + leadingZero(now.getMinutes()) + ' ' + a_p);
            }
        }

        var calcUtcOffset = function() {
            var now = new Date();
            var offset = now.getTimezoneOffset();
            $("#UtcOffset").val(offset);
        }
	</script>

    <h3>Schedule a tweet for <%=User.Identity.Name%></h3>

    <% if (!ViewData.ModelState.IsValid) { %>
        <div class="ui-widget">
			<div class="ui-state-error ui-corner-all" style="padding: 0 .7em; color: black;"> 
				<p style="color: black;"><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span> 
				    <%= Html.ValidationSummary("Could not schedule tweet. Please correct the errors and try again.") %>
				</p>
			</div>
		</div>
		<p style="clear: both;"></p>
    <% } %>
    
    <% Html.RenderPartial("TweetTimeline", "/Tweet/List"); %>

    <% using (Html.BeginForm()) {%>
    
        <%=Html.Hidden("UtcOffset", 0)%>

        <fieldset>
            <legend>Schedule a tweet</legend>
            <p>
                <label for="Status">Message:</label>
                <%= Html.TextArea("Status") %>
                <%= Html.ValidationMessage("Status", "*") %>
                <span id="status-chars-left">140</span>
            </p>
            <p>
                <label for="SendOn">Send on:</label>
                <%= Html.TextBox("SendOnDate", ViewData["SendOnDate"]) %>
                <%= Html.TextBox("SendOnTime", ViewData["SendOnTime"]) %>
            </p>
            <p>
                <button type="submit" value="Schedule">
                    Schedule tweet!
                </button>
            </p>
        </fieldset>

    <% } %>

    <p style="clear: both;">
        <%= Html.ActionLink("Back to list of scheduled tweets", "Index", "Tweet", null, new { @class = "more" })%>
    </p>
    
    <p style="clear: both;">&nbsp;</p>
</asp:Content>

